//
//  YRAuthCVController.swift
//  NightNews
//
//  Created by 薛永伟 on 2019/9/1.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class YRAuthCVController: UICollectionViewController {

    var dataSource:[Item]
    
    typealias ClickHandle = (_ item:Item)->()
    
    var clickHandle:ClickHandle
    
    init(items:[Item],clickHandle:@escaping ClickHandle) {
        
        self.clickHandle = clickHandle
        self.dataSource = items
        
        let layout = YREqualSpaceAlignFlowLayout.init(spaceBetweenCell: 25, alignType: .center)
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize.init(width: 40, height: 40)
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        collectionView.backgroundColor = .white
        
        collectionView.register(ItemCell.self, forCellWithReuseIdentifier: ItemCell.reuseID)

    }


    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataSource.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ItemCell.reuseID, for: indexPath) as! ItemCell
        
        let item = dataSource[indexPath.item]
        cell.imgView.image = UIImage.init(named: item.imgName)
        
        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let item = dataSource[indexPath.item]
        clickHandle(item)
    }
    
}


//MARK: - --------------类--------------
extension YRAuthCVController{
    
    /// 授权方式
    enum Item {
        
        case QQ
        case Wechat
        case Sina
        
        var imgName:String{
            switch self {
            case .QQ:
                return "Auth_qq"
            case .Wechat:
                return "Auth_wechat"
            case .Sina:
                return ""
            }
        }
    }
    
    class ItemCell:CodeLayoutCollectionViewCell {
        
        static let reuseID:String = "AuthItemCell"
        
        lazy var imgView: UIImageView = {
            let view = UIImageView()
            view.contentMode = .scaleAspectFit
            view.clipsToBounds = true
            return view
        }()
        
        override func codeCustomSubviews() {
            
            contentView.addSubview(imgView)
            imgView.snp.makeConstraints { (make) in
                make.width.height.equalTo(40.scaleLength)
                make.center.equalToSuperview()
            }
        }
    }
}
